package com.shixun.entity;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Vector;

public class TimeMaker {
	private static SimpleDateFormat formater;
	private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	private static SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy/MM/dd");
	private static SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
	private static SimpleDateFormat dateTimeFormat2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

	static {
		dateFormat.setLenient(false);// 这个的功能是不把1996-13-3 转换为1997-1-3
		dateTimeFormat.setLenient(false);
	}
	/**
	 * 当前时间========================================================================================================================
	 */
	/**指定格式的当前时间*/
	public static String nowByFormatStr(String formatStr){
		if(Checks.empty(formatStr))return "";
		formater = new SimpleDateFormat(formatStr);
		return formater.format(new Date());
	}
	/** 返回当前时间的java.sql.Timestamp格式*/
	public static Timestamp nowSqlTimestamp() {
		return new Timestamp(new Date().getTime());
	}
	/**返回当前时间的Timestamp格式*/
	public static Timestamp nowTimestamp() {
		return dateToTimestamp(new Date());
	}
	/**返回当前时间的java.sql.Date格式*/
	public static java.sql.Date nowSqlDate() {
		long ltime = System.currentTimeMillis();
		return new java.sql.Date(ltime);
	}
	/** @return yyyy-MM-dd*/
	public static String nowDate() {
		return dateFormat.format(new Date());
	}
	/** @return HH:mm:ss*/
	public static String nowTime() {
		return timeFormat.format(new Date());
	}
	/** @return yyyy-MM-dd HH:mm:ss */
	public static String nowDataTime() {
		return dateTimeFormat.format(new Date());
	}

	/**
	 * 常用格式 String To Date========================================================================================================================
	 */
	/** @parm yyyy-MM-dd*/
	public static Date dateStrToDate(String dateStr) {
		if (dateStr == null)return null;
		Date date = null;
		try {
			date = dateFormat.parse(dateStr); // Wed sep 26 00:00:00 CST 2007
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/** @parm yyyy-MM-dd  HH:mm:ss */
	public static Date dateTimeStrToDate(String dataTimeStr) {
		if (dataTimeStr == null)
			return null;
		Date date = null;
		try {
			date = dateTimeFormat.parse(dataTimeStr);
		} catch (ParseException e) {
//			e.printStackTrace();
			return null;
		}
		return date;
	}

	/**
	 * 常用格式 String To Timestamp
	 */
	/** @parm yyyy-MM-dd*/
	public static Timestamp dateStrToTimestamp(String dataTimeStr) {
		if (dataTimeStr == null) return null;
		Date date = dateTimeStrToDate(dataTimeStr.trim());
		if (date == null)return null;
		return new Timestamp(date.getTime());
	}
	/** @parm yyyy-MM-dd  HH:mm:ss */
	public static Timestamp dataTimeStrToTimestamp(String dateStr) {
		if (dateStr == null) return null;
		Date date = dateStrToDate(dateStr.trim());
		if (date == null)return null;
		return new Timestamp(date.getTime());
	}
	/**Date转Timestamp*/
	public static Timestamp dateToTimestamp(Date date) {
		if (date == null)
			return null;
		return new Timestamp(date.getTime());
	}
	/** 时间字符串转换Date*/
	public static Date parseStringToDate(String dateStr,String formatStr) {
		if(Checks.empty(dateStr))return null;
		if(Checks.empty(formatStr))return parseStringToDate(dateStr);
		formater = new SimpleDateFormat(formatStr);
		try {
			return formater.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
	/** 时间字符串转换Date*/
	public static Date parseStringToDate(String dateStr) {
		if (dateStr == null || dateStr.trim().equals(""))return null;
		dateStr = dateStr.trim();
		Date date = null;
		try {
			if (dateStr.contains("/")) {
				if (dateStr.length() <= 10) {
					date = dateFormat1.parse(dateStr);
				} else {
					date = dateTimeFormat2.parse(dateStr);
				}
			} else if (dateStr.contains("-")){
				if (dateStr.length() <= 10) {
					date = dateFormat.parse(dateStr);
				} else {
					date = dateTimeFormat.parse(dateStr);
				}
			}else
				return null;
		} catch (Exception e) {
			throw new IllegalArgumentException("你输入的日期不合法，请重新输入");
		}
		return date;
	}
	/**时间字符串转换java.sql.Timestamp*/
	public static Timestamp parseStringToSqlTimestamp(String dateStr) {
		Date date= parseStringToDate(dateStr);
		if(date!=null)
			return new Timestamp(date.getTime());
		return null;
	}

	/**
	 * 时间转换字符串========================================================================================================================
	 */
	/**
	 * 返回指定格式的转化
	 */
	public static String toFormatStr(Object theDate,String formatStr) {
		if (theDate == null)return "";
		if(Checks.empty(formatStr))return dateTimeFormat.format(theDate);
		formater=new SimpleDateFormat(formatStr);
		return formater.format(theDate);
	}
	/**
	 * 返回【yyyy-MM-dd HH:mm:ss】的格式
	 */
	public static String toDateTimeStr(Object theDate) {
		if (Checks.empty(theDate))return "";
		return dateTimeFormat.format(theDate);
	}

	/**
	 * 返回【yyyy-MM-dd】的格式
	 */
	public static String toDateStr(Object theDate) {
		if (theDate == null)return "";
		return dateFormat.format(theDate);
	}
	/**
	 * 返回【HH:mm:ss】的格式
	 */
	public static String toTimeStr(Object theDate) {
		if (theDate == null)return "";
		return timeFormat.format(theDate);
	}

	/**获取年份*/
	public static Integer getYear(Object theDate) {
		if (theDate == null)
			return null;
		dateTimeFormat = new SimpleDateFormat("yyyy");
		return Integer.parseInt(dateTimeFormat.format(theDate));
	}
	/**获取月份*/
	public static Integer getMonth(Object theDate) {
		if (theDate == null)
			return null;
		dateTimeFormat = new SimpleDateFormat("MM");
		return Integer.parseInt(dateTimeFormat.format(theDate));
	}
	/**获取日期日*/
	public static Integer getDay(Object theDate) {
		if (theDate == null)
			return null;
		dateTimeFormat = new SimpleDateFormat("dd");
		return Integer.parseInt(dateTimeFormat.format(theDate));
	}
	/**取昨天的日期*/
	public static java.sql.Date getYesterdaySQLDate() {
		long ltime = System.currentTimeMillis();
		return new java.sql.Date(ltime - 24 * 60 * 60 * 1000);
	}
	/**取昨天的日期*/
	public static Timestamp beforeDate(Date theDate){
		if (theDate == null)
			return null;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(theDate);
		calendar.roll(Calendar.DAY_OF_YEAR,-1);
		return new Timestamp(calendar.getTimeInMillis());
	}
	/**获取明天的日期*/
	public static Timestamp nextDate(Date theDate){
		if (theDate == null)return null;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(theDate);
		calendar.roll(Calendar.DAY_OF_YEAR,+1);
		return new Timestamp(calendar.getTimeInMillis());
	}
	
	public static int getBetweenDateNo(Date date1, Date date2){
		return getBetweenDate(date1, date2).length;
	}
	/**取中间的日期*/
	public static String[] getBetweenDate(Date date1, Date date2) {
		Vector<GregorianCalendar> v = new Vector<GregorianCalendar>();
		Vector<String> s= new Vector<String>();
		GregorianCalendar gc1 = new GregorianCalendar(), gc2 = new GregorianCalendar();
		gc1.setTime(date1);
		gc2.setTime(date2);
		do {
			GregorianCalendar gc3 = (GregorianCalendar) gc1.clone();
			v.add(gc3);
			s.add(dateFormat1.format(gc3.getTime()));
			gc1.add(Calendar.DAY_OF_MONTH, 1);
		} while (!gc1.after(gc2));
		return s.toArray(new String[s.size()]);
	}
	/**取星期几*/
	public static String getWeek(Date theDate){
		 String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
	        Calendar cal = Calendar.getInstance();
	        cal.setTime(theDate);
	        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
	        if (w < 0) w = 0;
	        return weekDays[w];

	}
	/**取时间差*/
	public static String getDValue(Date theDate1,Date theDate2){
		if(theDate1==null||theDate2==null )return "";
		long millisecond=theDate2.getTime()-theDate1.getTime();
		if(millisecond<0 )return "";
		if(millisecond==0 )return "0毫秒";
		int secondMs = 1000;
		int minuteMs = secondMs * 60;
		int hourMs = minuteMs * 60;
		int dayMs = hourMs * 24;
		long surplus=millisecond;//剩余
		long day = millisecond / dayMs;
		surplus-=day * dayMs;
		long hour = surplus / hourMs;
		surplus-= hour * hourMs;
		long minute = surplus / minuteMs;
		surplus-= minute * minuteMs;
		long second =surplus/ secondMs;
		surplus-= second * secondMs;
		long milliSecond = surplus;
		StringBuffer resBuffer=new StringBuffer();
		if(day!=0)resBuffer.append(day).append(" 天 ");
		if(hour!=0)resBuffer.append(hour).append(" 小时 ");
		if(minute!=0)resBuffer.append(minute).append(" 分 ");
		if(second!=0)resBuffer.append(second).append(" 秒 ");
		if(milliSecond!=0)resBuffer.append(milliSecond).append(" 毫秒 ");
		return resBuffer.toString();
	}
/*	public static void main(String[] a){
		System.out.println(TimeMaker.parseStringToSqlTimestamp("1992-10-29"));
	}*/
	public static String getCron(Object  date){
		String dateFormat="ss mm HH dd MM ? yyyy";
		return toFormatStr(date, dateFormat);
	}
	
	/**
	 * 前n小时
	 */
	public static String getBeforehour(int hour){
		Calendar calendar = Calendar.getInstance(); 
		calendar.add(Calendar.HOUR, hour);
		return dateTimeFormat.format(calendar.getTime());
	}
	/**
	 * 前n天
	 */
	public static String getBeforeDay(int day){
		Calendar calendar = Calendar.getInstance(); 
		calendar.add(Calendar.DATE, day);
		return dateTimeFormat.format(calendar.getTime());
	}
	/**
	 * 前n月
	 */
	public static String getBeforeMonth(int month){
		Calendar calendar = Calendar.getInstance(); 
		calendar.add(Calendar.MONTH, month);
		return dateTimeFormat.format(calendar.getTime());
	}
	
	/**
	 * 获取本周一时间(中国习惯)
	 * @return
	 */
	public static String getMonday(){
		Calendar cal =Calendar.getInstance();
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return dateFormat.format(cal.getTime());
	}
	
	/**
	 * 获取本周周末时间(中国习惯)
	 * @return
	 */
	public static String getSunday(){
		Calendar cal =Calendar.getInstance();
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		return dateFormat.format(cal.getTime());
	}
	
	
	
	public static void main(String[] a){
		System.out.println(TimeMaker.getMonday());
		System.out.println(TimeMaker.getSunday());
	}
}
